package com.loie.xcx.web.controller;



import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.model.bo.CustomerBO;
import com.loie.xcx.service.SysAttachmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-09-22
 */
@Slf4j
@RestController
@RequestMapping("/report")
public class SysReporttemplateController {
    @Autowired
    private SysAttachmentService sysAttachmentService;


    @PostMapping("/reportdbtofile")

    public JsonResult<List<String>> reportdbtofile(@RequestParam String msg) throws Exception {
        JSONObject msgJson = JSONObject.parseObject(msg);
       String  salesorder=msgJson.getString("salesorder");
        JSONArray array=new JSONArray();
        array = JSONArray.parseArray(msgJson.getString("Columns"));
        List<String> recordlist = new ArrayList<>();
        for (int i=0;i<=array.size()-1;i++){
            recordlist.add(array.getJSONObject(i).getString("recordid"));}


        //String recordid = msgJson.getString("recordid");
        //System.out.println(recordlist);
        log.info("************外销合同："+salesorder+"报表附件上传开始*************");
        Boolean issucess = sysAttachmentService.reportdbtofile(recordlist);
        if (issucess) {
            JsonResult<List<String>> result = new JsonResult<>();
            result.setData(recordlist);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(result);
            log.info(json.toString());
            log.info("************外销合同："+salesorder+"报表附件上传完成*************");
            return new JsonResult<>(recordlist);

        }
        else
            return new JsonResult<>(400, "查询失败");

    }

}

